#include <iostream>
#include <vector>
#include "MediaCenter.h"
#include "SonyPlayerFactory.h"
#include "SamsungPlayerFactory.h"
#include "JVCPlayerFactory.h"
#include "PlayerFactory.h"
using namespace std;

int main() {
	MediaCenter mc;
	PlayerFactory*	jvcFactory = new JVCPlayerFactory;
	PlayerFactory*	sonyFactory = new SonyPlayerFactory;
	PlayerFactory*	samsungFactory = new SamsungPlayerFactory;
	
	vector<Player*> allplayers;
	
	char input;
	unsigned int input1;
	char input2;
	
	while(1) {
		cout <<
			"\n| Jvc | Sony | saMsung | Choose | q: ";

		char c;
		cin >> c;

		if(c!='J' && c!='S' && c!='M' && c!='C' && c!='q') {
				cout << "No such command. Try again (q to exit)." << endl;
				continue;
		}

		if (c == 'q') {
			break;
		}

		switch (c) {
		case 'J' :
			cout << "Please choose a device to create (Dvd, Vcr, Cd): " << endl;
			cin >> input;
			
			if (input == 'D') {
				allplayers.push_back(jvcFactory->FDVDPlayer(&mc));
			} else if (input == 'V') {
				allplayers.push_back(jvcFactory->FVCRPlayer(&mc));
			} else if (input == 'C') {
				allplayers.push_back(jvcFactory->FCDPlayer(&mc));
			} else {
				cout << "Illegal device" << endl;
				break;
			}
			
			cout << "Added player " << allplayers.size() << endl;;
			
			break;

		case 'S' :  
			cout << "Please choose a device to create (Dvd, Vcr, Cd): " << endl;
			cin >> input;
			
			if (input == 'D') {
				allplayers.push_back(sonyFactory->FDVDPlayer(&mc));
			} else if (input == 'V') {
				allplayers.push_back(sonyFactory->FVCRPlayer(&mc));
			} else if (input == 'C') {
				allplayers.push_back(sonyFactory->FCDPlayer(&mc));
			} else {
				cout << "Illegal device" << endl;
				break;
			}
			
			cout << "Added player " << allplayers.size() << endl;;
			
			break;

		case 'M' :
			cout << "Please choose a device to create (Dvd, Vcr, Cd): " << endl;
			cin >> input;
			
			if (input == 'D') {
				allplayers.push_back(samsungFactory->FDVDPlayer(&mc));
			} else if (input == 'V') {
				allplayers.push_back(samsungFactory->FVCRPlayer(&mc));
			} else if (input == 'C') {
				allplayers.push_back(samsungFactory->FCDPlayer(&mc));
			} else {
				cout << "Illegal device" << endl;
				break;
			}
			
			cout << "Added player " << allplayers.size() << endl;;
			
			break;

		case 'C' :  
			cout << "Please choose a player to make an action on (0 for Media Center, or 1-" << allplayers.size() << " for players):"  << endl;
			cin >> input1;
			
			if (input1 < 0 || input1 > allplayers.size()) {
				cout << "Illegal device" << endl;
				break;
			}
			
			cout << "Please choose an action (Play, Stop, Rewind, Forward): " << endl;
			cin >> input2;
			
			if (input1 == 0 && input2 == 'P') {
				cout << "Playing using Media Center not supported in this tester." << endl;
				break;
			}
			
			if (input2 == 'P') {
				allplayers[input1-1]->start();
			} else if (input2 == 'S') {
				if (input1 == 0) { mc.stop(); }
				else { allplayers[input1-1]->stop(); }
			} else if (input2 == 'R') {
				if (input1 == 0) { mc.rewind(); }
				else { allplayers[input1-1]->rewind(); }
			} else if (input2 == 'F') {
				if (input1 == 0) { mc.forward(); }
				else { allplayers[input1-1]->forward(); }
			} else {
				cout << "Illegal action" << endl;
				break;
			}
			
			break;
		}
	}

	for (unsigned int i=0; i < allplayers.size(); i++) {
		delete allplayers[i];
	}
	
	delete jvcFactory;
	delete sonyFactory;
	delete samsungFactory;
	
	return 0;
}
